package com.wk.code.wkserver.controller.common;

import com.wk.code.wkserver.utils.JwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wq
 * @version 1.0
 * @date 2021/07/27 17:55
 */
public class BaseController {

    private HttpServletRequest request;

    @ModelAttribute
    void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    /**
     * 获取前台用户ID
     *
     * @return Integer
     */
    public Integer getUserId() {
        String userId = JwtUtil.getUserId(request.getHeader("token"));
        if (StringUtils.isNotEmpty(userId)) {
            return Integer.parseInt(userId);
        }
        return null;
    }


    /**
     * 获取系统管理用户ID
     *
     * @return Integer
     */
    public Integer getSysUserId() {
        String userId = JwtUtil.getSysUserId(request.getHeader("token"));
        if (StringUtils.isNotEmpty(userId)) {
            return Integer.parseInt(userId);
        }
        return null;
    }
}
